package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoImages;
import com.anhuanjia.tools.eao.bean.EaoOpinion;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 反馈与支持操作类
 * 
 * @author Administrator
 *
 */
@Alias(value = "retroaction")
public class OpinionUI extends UIFacade {

	/**
	 * 添加/更改反馈与支持表
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		Transaction transaction = new Transaction();
		String loginUserId = Long.toString(Member.getUID());
		String loginUserName = Member.getRealName();
		Date data = new Date();
		EaoOpinion opinion = new EaoOpinion();
		opinion.setValue(this.Request);
		opinion.setCreateTime(data);
		opinion.setUpdateTime(data);
		opinion.setIsDelete(0);
		opinion.setState(0);// 0创建1已查看...
		opinion.setMemberId(loginUserId);
		opinion.setOperator(loginUserName);
		transaction.insert(opinion);
		if (transaction.commit()) {
			s("添加成功");
		} else {
			f("添加失败");
		}

	}

	/**
	 * 查询所有的反馈与支持数据
	 */
	@Priv(loginType = LoginType.Member)
	public void allOpinions() {
		int pageSize = this.Request.getInt("PageSize");
		if (pageSize == 0) {
			pageSize = 10;
		}
		int pageIndex = this.Request.getInt("PageIndex");
		Q q = new Q("select count(Id) as Num from eao_opinion where IsDelete = 0");

		DataTable count = q.fetch();
		Long countNum = (Long) count.get(0).get("Num");
		DataTable dt = new Q("select * from eao_opinion where IsDelete = 0 order by CreateTime desc").fetch(pageSize,
				pageIndex);
		List<EaoOpinion> opinionList = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoOpinion opinion = new EaoOpinion();
			opinion.setValue(dataRow);
			opinionList.add(opinion);
		}
		this.Response.put("tatal", countNum);
		this.Response.put("data", opinionList);
	}

	/**
	 * 查询一条反馈与支持数据
	 */
	@Priv(loginType = LoginType.Member)
	public void findOneOpinion() {
		String id = $V("Id");
		if (StringUtils.isEmpty(id)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		DataTable dt = new Q("select * from eao_opinion where Id = ? ", id).fetch();
		if (dt.getRowCount() != 0) {
			EaoOpinion opinion = new EaoOpinion();
			opinion.setValue(dt.get(0));
			DataTable dt_img = new Q("select * from eao_images where ParentId = ? ", id).fetch();
			List<EaoImages> imgList = new ArrayList<>();
			for (DataRow dataRow : dt_img) {
				EaoImages img = new EaoImages();
				img.setValue(dataRow);
				imgList.add(img);
			}
			this.Response.put("opinion", opinion);
			this.Response.put("imgList", imgList);
		} else {
			f("查询失败,数据不存在!");
		}

	}

	/**
	 * 删除数据
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteOpinion() {
		Transaction trans = new Transaction();
		String ids = $V("Ids");
		if (StringUtils.isEmpty(ids)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		trans.add(new Q(" update eao_opinion  set IsDelete = 1 where ").in("Id", ids));
		trans.add(new Q(" delete from eao_images where ").in("ParentId", ids));
		if (trans.commit()) {
			s("删除成功");
		} else {
			f("删除失败");
		}

	}

	/**
	 * 删除图片数据
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteImages() {
		Transaction trans = new Transaction();
		String ids = $V("Ids");
		if (StringUtils.isEmpty(ids)) {
			f("操作失败，请确认操作信息!");
			return;
		}
		trans.add(new Q(" delete from eao_images where ").in("Id", ids));
		if (trans.commit()) {
			s("删除成功");
		} else {
			f("删除失败");
		}
	}

	/**
	 * 查询图片数据
	 */
	@Priv(loginType = LoginType.Member)
	public void findByImages() {
		String ids = $V("Id");
		List<EaoImages> list = new ArrayList<EaoImages>();
		if (StringUtil.isNotEmpty(ids)) {
			DataTable dataTable = new Q(" select * from eao_images where ParentId = ?", ids).fetch();
			for (DataRow dataRow : dataTable) {
				EaoImages images = new EaoImages();
				images.setValue(dataRow);
				list.add(images);
			}
		}
		this.Response.put("image", list);
	}

}
